package com.example.yeujian5.yuejian5.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yeujian5.yuejian5.Listenner.PersonListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.activity.EditActivity;
import com.example.yeujian5.yuejian5.activity.LoginActivity;
import com.example.yeujian5.yuejian5.activity.UserCollectActivity;
import com.example.yeujian5.yuejian5.activity.UserPhotoActivity;
import com.example.yeujian5.yuejian5.activity.UserSignActivity;
import com.example.yeujian5.yuejian5.bean.UserBean;
import com.example.yeujian5.yuejian5.model.UserModel;
import com.squareup.picasso.Picasso;

/**
 * Created by 7h0rns on 2017/11/9.
 */

public class PersonFragment extends Fragment {
    private TextView phone_number, name;
    private ImageView my_thum, My_shaitu, My_collect, My_order;
    private Button edit, exit;
    private String userid;
    ProgressDialog loadingDialog;
    private static int MSG_DISMISS_DIALOG=0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);
        initViews(view);
        userid = getActivity().getSharedPreferences("Userid", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取用户id", String.valueOf(userid));
        initEvents();
        showData();
        return view;
    }

    private void initViews(View view){
        phone_number = (TextView)view.findViewById(R.id.person_tel);
        name = (TextView)view.findViewById(R.id.person_name);
        my_thum = (ImageView) view.findViewById(R.id.person_img);
        edit = (Button)view.findViewById(R.id.person_edit);
        exit = (Button)view.findViewById(R.id.person_out);
        My_collect = (ImageView) view.findViewById(R.id.per_collect_image);
        My_shaitu = (ImageView) view.findViewById(R.id.per_photo_image);
        My_order = (ImageView) view.findViewById(R.id.per_buy_image);
    }

    private void initEvents(){
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showListDialog();
            }
        });

        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),EditActivity.class);
                startActivity(intent);
            }
        });

        My_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserCollectActivity.class);
                startActivity(intent);
            }
        });

        My_shaitu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserPhotoActivity.class);
                startActivity(intent);
            }
        });

        My_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserSignActivity.class);
                startActivity(intent);
            }
        });
    }

    private void showData() {
        UserModel userModel = new UserModel();
        userModel.getUserByID(null, userid, personListener);
    }

    PersonListener personListener = new PersonListener() {
        @Override
        public void onResponse(Object beanobject) {
            phone_number.setText(((UserBean)beanobject).getTel());
            name.setText(((UserBean)beanobject).getUser_name());
            String address="http://172.24.10.175/workout/Uploads/"+((UserBean)beanobject).getPhoto();
            if(!"".equals(((UserBean)beanobject).getPhoto())){
                Picasso.with(getActivity())
                        .load(address)
                        .placeholder(R.drawable.touxiang)
                        .noFade()
                        .into(my_thum);
            }
        }
        @Override
        public void onFail(String msg){
        }
    };

    private void showListDialog() {
        final String[] items = {"注销"};
        AlertDialog.Builder listDialog = new AlertDialog.Builder(getActivity());
        listDialog.setTitle("个人设置");

        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showLoadingDialog();
                mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, 1000);
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                getActivity().startActivity(intent);
                getActivity().finish();
            }
        });
        listDialog.show();
    }

    private Handler mHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if(MSG_DISMISS_DIALOG == msg.what){
                if(null != loadingDialog){

                    if(loadingDialog.isShowing()){
                        loadingDialog.dismiss();
                    }
                }
            }
        }
    };

    private void showLoadingDialog() {
        loadingDialog = new ProgressDialog(getActivity());
//        loadingDialog.setTitle("test");
        loadingDialog.setMessage("正在退出...");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(false);
        loadingDialog.show();
    }

}
